<?php
class itemAction extends baseAction {

	function index(){
		$rs = D("ju_item");
		
		import("@.ORG.Page");       //导入分页类
		$pageSize = 10;
		$totalNums = $rs->count("id");
		$Page = new Page($totalNums, $pageSize);
        
        // 模拟设置分页额外传入的参数
        //$Page->parameter    =   'search=key&name=thinkphp';
        // 设置分页显示
        $Page->setConfig('header', '条数据');
        $Page->setConfig('first', '<<');
        $Page->setConfig('last', '>>');
        $page = $Page->show();
		//$limit = ($page-1) * $pageSize .",". $pageSize;
		
		$items = $rs->field("ju_item.*,ju_class.className")->join("left join ju_class on ju_class.id=ju_item.classId")->limit($Page->firstRow. ',' . $Page->listRows)->order('ju_item.sort desc')->select();
		
		//echo $rs->getLastSql();		
		
		$this->assign("items",$items);
		$this->assign("page",$page);
		//print_r($p);
		//$a = array('status'=>'1',"msg"=>"成功");
		//echo $_GET['callback']."(".json_encode($a).")";
		$this->display();
	}
	function save(){
		$rs = D("ju_item");
		
		$r = $rs->insert($_POST);
		if(1==$r)
			echo "添加成功！<a href='/admin.php?c=item&a=list'>列表</a>";
	}

	public function edit(){
		$rs = D("ju_item");
		$item = $rs->where("id=".$_GET['id'])->select();
		
		
		$this->assign("item",$item[0]);
		$this->display();
	}
	
	public function update(){
		$rs = D("ju_item");
		//print_r($_POST);
		
		$r = $rs->save($_POST);
		//echo $rs->getLastSql();
		$this->success("修改成功！","/admin.php?m=item");
		//echo "修改成功！<a href='/admin.php?m=item&a=index'>列表</a>";
	}
	
	public function del(){
		$rs = D("ju_item");
		//print_r($_POST);
		if($_POST['id']==''){
			$this->error("貌似你没有选择要删除的东西把。蛋疼！");
			
		}
		$delId = implode(",",$_POST['id']);
		//echo $delId;
		$r = $rs->where("id in(".$delId.")")->delete();
		//echo $rs->getLastSql();
		
		if($r>=1){
			echo "删除成功！<a href='/admin.php?m=item'>返回</a>";
		}else{
			$this->error("操作失败。请联系小羊。蛋疼！");
		}
	}
	
	public function getItemInfo($item_id){
		
		
		$c = $this->taobao_client();
		
		$req = $c->load_api('ItemGetRequest');
		
		$req->setFields("detail_url,title,nick,price,pic_url,num_iid,");
		$req->setNumIid($item_id);
		$resp = $c->execute($req);
		
		$resp = (array)$resp;
		$resp = (array)$resp[item];
		
		return $resp;
		
		//objectToArray($resp);
		
		//print_r($resp);
	}
	
	public function mutiAdd(){
		if($_POST['item_ids']=='')
			$this->display();
		else{
			$itemInfos = array();
			$item_ids = $_POST['item_ids'];
			$item_ids = explode(',',$item_ids);
			foreach($item_ids as $k=>$v){
				$temp = $this->getItemInfo($v);
				if($temp!="")
					$itemInfos[] = $this->getItemInfo($v);
			}
			$this->assign("itemInfos",$itemInfos);
			$this->display();
		}
	}
	function mutiGet(){
		$itemInfos = array();
		$item_ids = $_POST['item_ids'];
		$item_ids = explode(',',$item_ids);
		foreach($item_ids as $k=>$v){
			$itemInfos[] = $this->getItemInfo($v);
		}
		$this->assign("itemInfos",$itemInfos);
		$this->display();
	}
	public function mutiSave(){
	
		$rs = D("ju_item");
		
		foreach($_POST['item_id'] as $k=>$v){
			$data = array(
							'item_id'=>$_POST['item_id'][$k],
							'title'=>$_POST['title'][$k],
							'item_link'=>$_POST['item_link'][$k],
							'pic_url'=>$_POST['pic_url'][$k],
							'shop'=>$_POST['shop'][$k],
							'price'=>$_POST['price'][$k],
							'sale'=>$_POST['sale'][$k],
							'desc'=>$_POST['desc'][$k],
							'classId'=>$_POST['classId'],
						);
			//print_r($data);
			$r = $rs->add($data);
			//echo $r;
			//echo $rs->getLastSql();
			if($r>0){
				echo "成功添加一条！<br/>";
			}else{
				echo "fail";
			}
		}
		echo "<a href='/admin.php?m=item&a=mutiAdd'>继续添加</a>";
		
	}
	
	public function status(){
		$rs = D("ju_item");
		$r = $rs->where("id=".$_GET['id'])->find();
		if($r[$_GET['type']]){
			$rs->where("id=".$_GET['id'])->save(array($_GET['type']=>0));
			//echo $rs->getLastSql();
			echo 0;
		}else{
			$rs->where("id=".$_GET['id'])->save(array($_GET['type']=>1));
			echo 1;
		}
		
		//echo $rs->getLastSql();
		//print_r($r);
		
	}

	
	
	public function test(){
		$a = array('status'=>'1',"msg"=>"成功");
		echo $_GET['callback']."(".json_encode($a).")";
		
		$t = new ju_item();
		
		$a = $t->where("1=1")->sel();
		print_r($a);
	}

//end of the class
}

?>